/**
 * 
 */
package org.dragonfire.util.token;

import java.util.ArrayList;

/**
 * @author Darth Android
 * @date Sep 2, 2007
 */
public class Token implements Comparable<Token> {

	public static final Token UNDEFINED = new Token(new Byte[] {
			(byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF
	}, "Undefined", "{UNDEFINED}", "{UNDEFINED}");
	private Byte[] bytes;
	private String name;
	private String editText;
	private String displayText;

	/**
	 * @param bytes
	 * @param name
	 * @param editText
	 * @param displayText
	 */
	public Token(Byte[] bytes, String name, String editText, String displayText) {
		super();
		this.bytes = bytes;
		this.name = name;
		this.editText = editText;
		this.displayText = displayText;
	}

	public int getSize() {
		return getBytes().length;
	}

	/**
	 * @return the bytes
	 */
	public Byte[] getBytes() {
		return bytes;
	}

	/**
	 * @return the name
	 */
	public String getName() {
		return name;
	}

	/**
	 * @return the editText
	 */
	public String getEditText() {
		return editText;
	}

	/**
	 * @return the displayText
	 */
	public String getDisplayText() {
		return displayText;
	}

	public String toString() {
		return getDisplayText();
	}

	public static String toEditString(Token[] tokens) {
		String output = "";
		for (Token t : tokens) {
			output += t.getEditText();
		}
		return output;
	}

	public static String toDisplayString(Token[] tokens) {
		String output = "";
		for (Token t : tokens) {
			output += t.getDisplayText();
		}
		return output;
	}

	public static Byte[] toByteArray(Token[] tokens) {
		ArrayList<Byte> bytes = new ArrayList<Byte>();
		for (Token t : tokens) {
			for (Byte b : t.getBytes()) {
				bytes.add(b);
			}
		}
		return bytes.toArray(new Byte[bytes.size()]);
	}

	public int compareTo(Token t) {
		if (getEditText().length() == t.getEditText().length())
			return getEditText().compareToIgnoreCase(t.getEditText());
		return getEditText().length() - t.getEditText().length();
	}
}
